﻿/*---------------------------------------  
/// <summary>
/// program design：agui 
/// datetime：2009.05.20
/// art aegis：http://dreamman_yx.blog.163.com
/// </summary>
---------------------------------------*/
var Agui_LastImageDiv = null;
var currentImage = null;
var currentFolder = null;
var _str1="您已经选择了这副图片!";
var _str2="文件名太长!";
var _str3="IE7不能显示<br />本地图片!";
var _str4="不是IE版本<br />不能显示本<br />地图片!";
var _str5="创建文件夹时你必须输入一个正确的名称!";
var _str6="您必须选择一个正确的文件!";
var _str7="选择文件夹";
var _str8="选择图片";
var Agui_EventCache = [];
function Agui_AddEvents(obj, evTypes, fn) {
	for (i=0; i<evTypes.length; i++) Agui_AddEvent(obj, evTypes[i], fn);
};
function Agui_AddEvent(obj, evType, fn, useCapture) {
	//if (useCapture==undefined) 
	useCapture=true;
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, useCapture);
	} else if (obj.attachEvent) {
		obj.attachEvent('on'+evType, fn);
	}
	Agui_EventCache.push([obj, evType, fn, useCapture]);
};
function Agui_RemoveEvent(obj, evType, fn, useCapture) {
    if (useCapture==undefined) useCapture=true;
	if (obj.removeEventListener) {
      obj.removeEventListener(evType, fn, useCapture);
    } else if (obj.detachEvent) {
      obj.detachEvent('on' + evType, fn);
    }
};
function Agui_Unload() {
    for (var i = 0; i < Agui_EventCache.length; i++) {
      Agui_RemoveEvent.apply(this, Agui_EventCache[i]);
      Agui_EventCache[i][0] = null;
    }
    Agui_EventCache = false;	
};
Agui_AddEvent(window,'unload',Agui_Unload, false);

/*  API for holding all FreeTextBox's
--------------------------------------- */
var Agui_API = new Object();
var Agui_Names = [];

/* Browser Detection 'Agui_Browser'
--------------------------------------- */
function Agui_BrowserDetect() {
	doc=window.document;
	navVersion=navigator.appVersion.toLowerCase();
	this.ie4=(!doc.getElementById&&doc.all)?true:false;
	this.ie5=(navVersion.indexOf("msie 5.0")!=-1)?true:false;
	this.ie55=(navVersion.indexOf("msie 5.5")!=-1)?true:false;
	this.ie6=(navVersion.indexOf("msie 6.0")!=-1)?true:false;
	this.ie7=(navVersion.indexOf("msie 7.0")!=-1)?true:false;
	this.isIE=(this.ie5||this.ie55||this.ie6||this.ie7)?true:false;
	this.isGecko=!this.isIE;
};
Agui_Browser = new Agui_BrowserDetect();

/* OOP Timeout Manager 'Agui_Timeout'
--------------------------------------- */
function Agui_TimeoutManager() {
	this.pendingCalls = {};		
};
Agui_TimeoutManager.prototype.addMethod = function(name,obj,method,delay,arg1,arg2) {
	this.clearMethod(name);
	this.pendingCalls[name] = new Agui_TimeoutCall(obj,method,arg1,arg2);
	this.pendingCalls[name].timeout = 
		setTimeout('Agui_Timeout.executeMethod("' + name + '");',delay);
};
Agui_TimeoutManager.prototype.executeMethod = function(name) {
	call = this.pendingCalls[name];
	if (call != null) {
		call.obj[call.method](call.arg1,call.arg2);
		this.clearMethod(name);
	}
};
Agui_TimeoutManager.prototype.clearMethod = function(name) {
	if (this.pendingCalls[name]) 
		delete this.pendingCalls[name];
};
//* Object to hold timeout reference
function Agui_TimeoutCall(obj,method,arg1,arg2) {
	this.obj = obj;
	this.method = method;
	this.arg1 = arg1;
	this.arg2 = arg2;
	this.timeout = null;
};
Agui_Timeout = new Agui_TimeoutManager();

/* Constants 
----------------------------------------- */
Agui_MODE_HTML = 0;
Agui_MODE_DESIGN = 1;
Agui_MODE_PREVIEW = 2;
//
Agui_PASTE_DEFAULT = 0;
Agui_PASTE_DISABLED = 1;
Agui_PASTE_TEXT = 2;
//
Agui_TAB_DISABLED = 0;
Agui_TAB_NEXTCONTROL = 1;
Agui_TAB_INSERTSPACES = 2;
//
Agui_BUTTON_ON = 0;
Agui_BUTTON_OFF = 1;
//
Agui_BREAK_P = 0;
Agui_BREAK_BR = 1;
//
Agui_KEY_TAB = 9;
Agui_KEY_ENTER = 13;
Agui_KEY_QUOTE = 222;
Agui_KEY_V = 86;
Agui_KEY_P = 86;
Agui_KEY_B = 66;
Agui_KEY_I = 73;
Agui_KEY_U = 85;
Agui_KEY_Z = 90;
Agui_KEY_Y = 89;
//
Agui_CODE_OPENCURLY = '&#8220;';
Agui_CODE_CLOSECURLY = '&#8221;';
//
Agui_BUTTON_STYLEDBACKGROUNDS = 0;
Agui_BUTTON_IMAGEBACKGROUNDS = 1;


/* Misc Methods
------------------------------------------ */
function Agui_SetListValue(list, value, checkText) {
	checkText = checkText || false;
	value = String(value).toLowerCase();

	for (var i=0; i<list.options.length; i++) {
		if (list.options[i].value.toLowerCase() == value || (checkText && list.options[i].text.toLowerCase() == value)) {
			list.selectedIndex = i;
			return;
		}
	}
};
function Agui_ParseUnit(styleString) {
	var unit = new Object();
	unit.value = 0;
	unit.unitType = '';
	for(var i=0; i<styleString.length; i++) {
		if (isNaN(styleString.charAt(i)))
			unit.unitType += styleString.charAt(i);
		else 
			unit.value = parseInt(unit.value.toString() + styleString.charAt(i));
	}
	return unit;
};
function Agui_DecToHex(dec) {
	return parseInt(dec).toString(16); 
};
function Agui_RgbToHex(r,g,b) {
	return "#" + Agui_IntToHex(r) + Agui_IntToHex(g) + Agui_IntToHex(b);
};
function Agui_IntToHexColor( intColor ) {
	if (!intColor) return null;
	intColor = intColor.toString(16).toUpperCase();
	while (intColor.length < 6) intColor = "0" + intColor;
	return "#" + intColor.substring(4,6) + intColor.substring(2,4) + intColor.substring(0,2);
};
function Agui_RgbStringToHex(rgbString){ 
	var r, g, b;
	rgbString = rgbString.toString().toLowerCase();	
	if(rgbString.indexOf("rgb(") == -1 || rgbString.indexOf(")") == -1) return rgbString;
	
	rgbString = rgbString.substring(rgbString.indexOf("(")+1, rgbString.indexOf(")")-1);
	rgb = rgbString.split(',');
	r = rgb[0];
	g = rgb[1];
	if (rgb.length == 2) b = rbg[2]; else b = 0;	
	return Agui_RgbToHex(r,g,b);
};
function Agui_IntToHex(dec){ 
	var result = (parseInt(dec).toString(16)); 
	if(result.length ==1) 
	result= ("0" +result); 
	return result.toUpperCase(); 
};
function Agui_GetQueryStringValues(url) {	
	url = new String(url);
	
	var queryStringValues=new Object();
	var querystring=url.substring(url.indexOf('?')+1, url.length);
	var querystringSplit = querystring.split('&');
	
	for (i=0; i < querystringSplit.length; i++){
		var pair=querystringSplit[i].split('=');
		var name=pair[0];
		var value=pair[1];
		queryStringValues[ name ]=value;
	}
	return queryStringValues;
};
/* Static Popup HTML
---------------------------------------- */
var Agui_PopUpStyle = "\
html, body { \
	background-color: #ECE9D8; \
	color: #000000; \
	font: 11px Tahoma,Verdana,sans-serif; \
	padding: 0px; \
} \
body { margin: 5px; } \
form { margin: 0px; padding: 0px;} \
table { \
  font: 11px Tahoma,Verdana,sans-serif; \
} \
form p { \
  margin-top: 5px; \
  margin-bottom: 5px; \
} \
h3 { margin: 0; margin-top: 4px;  margin-bottom: 5px; font-size: 12px; border-bottom: 2px solid #90A8F0; color: #90A8F0;} \
.fl { width: 9em; float: left; padding: 2px 5px; text-align: right; } \
.fr { width: 7em; float: left; padding: 2px 5px; text-align: right; } \
fieldset { padding: 0px 10px 5px 5px; } \
button { width: 75px; } \
select, input, button { font: 11px Tahoma,Verdana,sans-serif; } \
.space { padding: 2px; } \
.title { background: #ddf; color: #000; font-weight: bold; font-size: 120%; padding: 3px 10px; margin-bottom: 10px; \
border-bottom: 1px solid black; letter-spacing: 2px; \
} \
.f_title { text-align:right; }\
.footer { border-top:2px solid #90A8F0; padding-top: 3px; margin-top: 4px; text-align:right; }\
";

var Agui_PopUpHeader = new String("<html><body> \
<head>\
<title>POPUP_TITLE</title>\
<style type='text/css'>\
html, body { \
	background-color: #ECE9D8; \
	color: #000000; \
	font: 11px Tahoma,Verdana,sans-serif; \
	padding: 0px; \
} \
body { margin: 5px; } \
form { margin: 0px; padding: 0px;} \
table { \
  font: 11px Tahoma,Verdana,sans-serif; \
} \
form p { \
  margin-top: 5px; \
  margin-bottom: 5px; \
} \
h3 { margin: 0; margin-top: 4px;  margin-bottom: 5px; font-size: 12px; border-bottom: 2px solid #90A8F0; color: #90A8F0;} \
.fl { width: 9em; float: left; padding: 2px 5px; text-align: right; } \
.fr { width: 7em; float: left; padding: 2px 5px; text-align: right; } \
fieldset { padding: 0px 10px 5px 5px; } \
button { width: 75px; } \
select, input, button { font: 11px Tahoma,Verdana,sans-serif; } \
.space { padding: 2px; } \
.title { background: #ddf; color: #000; font-weight: bold; font-size: 120%; padding: 3px 10px; margin-bottom: 10px; \
border-bottom: 1px solid black; letter-spacing: 2px; \
} \
.f_title { text-align:right; }\
.footer { border-top:2px solid #90A8F0; padding-top: 3px; margin-top: 4px; text-align:right; }\</style>\
<script type='text/javascript'>\
POPUP_SCRIPT\
</script>\
</head>\
<body>\
<form action=''> \
<h3>POPUP_TITLE</h3> \
POPUP_HTML \
</form> \
</body> \
</html>");


/* Use Function
---------------------------------------- */
var Agui_index_img="";
function Agui_PreviewImageSrc()
{
    var img_web_cbx=document.getElementById("img_web_cbx");
    var img_web_url=document.getElementById("img_web_url");
    if(img_web_cbx.checked)
    {
        var img_tmp_flag=img_web_url.value.lastIndexOf("/");
        var img_tmp_url_path=img_web_url.value.substr(img_web_url.value,img_tmp_flag);
        var img_tmp_url_name=img_web_url.value.substring(img_tmp_flag+1,img_web_url.value.length);
        Agui_PreviewImage(null,img_tmp_url_path,img_tmp_url_name,'unknow');
    }
    else
    {       
        var img_tmp_flag=Agui_index_img.lastIndexOf("/");
        var img_tmp_url_path=Agui_index_img.substr(Agui_index_img,img_tmp_flag);
        var img_tmp_url_name=Agui_index_img.substring(img_tmp_flag+1,Agui_index_img.length);
        Agui_PreviewImage(null,img_tmp_url_path,img_tmp_url_name,'unknow');     
    }
}
function Agui_HightlightDiv(theDiv) {
    if (Agui_LastImageDiv) {
	    Agui_LastImageDiv.style.border = "1px solid #CCCCCC";
	    Agui_LastImageDiv.style.padding = "1px";
    }
    Agui_LastImageDiv = theDiv;
    theDiv.style.border = "2px solid #316AC5";
    theDiv.style.padding = "0";
};
function Agui_InsertImage() {
	
	image = document.getElementById('img_preview');
	src = document.getElementById('img_preview').src;
	if (src == '' || src == null) return;
	
	alt = document.getElementById('img_alt').value;
	title = document.getElementById('img_title').value;
	width = image.width;                             //document.getElementById('img_width').value;
	height = image.height;                           //document.getElementById('img_height').value;
	align = document.getElementById('img_align').options[document.getElementById('img_align').selectedIndex].value;
	
	hspace = document.getElementById('img_hspace').value;
	vspace = document.getElementById('img_vspace').value;
	border = document.getElementById('img_border').value;

	img = '<img src="' + src + '"' + ' temp_src="' + src + '"' + 
		( (alt != '') ? ' alt="' + alt + '"' : '' ) + 
		( (title != '') ? ' title="' + title + '"' : '' ) + 
		( (width != '') ? ' width="' + width + '"' : '' ) + 
		( (height != '') ? ' height="' + height + '"' : '' ) + 
		( (height != '') ? ' height="' + height + '"' : '' ) + 
		( (align != '') ? ' align="' + align + '"' : '' ) + 
		( (hspace != '') ? ' hspace="' + hspace + '"' : '' ) + 
		( (vspace != '') ? ' vspace="' + vspace + '"' : '' ) + 
		( (border != '') ? ' border="' + border + '"' : '' ) + 
		' />';

	var ishide = true;
    if(arguments.length==0)
    {
        if(typeof(window.opener)=="undefined")
        {
            window.parent.document.selection.createRange().pasteHTML(img)   
        }
        else
        {
            window.opener.document.selection.createRange().pasteHTML(img)    
        }
        window.returnValue = null;
    }
    else if(arguments.length==2)
    {
        switch(arguments[1])
        {
            case "text":
                var execstr = arguments[0] + ".value=\"" + src + "\";";
                eval(execstr);
                ishide = false;
                break;
            case "object":
                var execstr=arguments[0]+".InsertHtml(img);"
                eval(execstr);
                break;
            case "function":
                var execstr=arguments[0]+"('"+src+"');"
                eval(execstr);
                break;
            case "func-obj":
                var execstr=arguments[0]+"(img);"
                eval(execstr);
                break;
            default:
                alert("插入图片错误，您没有设置正确的参数！");
                break;
        }
    }
    //关闭父级窗口(showhide在当前窗口或popit.js文件中)
    if(typeof(window.parent.showhide)!="undefined")
    {
        window.parent.showhide(ishide);
    }
};
function Agui_DeleteImage(galleryID) {
	__doPostBack(galleryID,"DeleteImage:" + currentImage);
};
function Agui_DeleteFolder(galleryID) {
	__doPostBack(galleryID,"DeleteFolder:" + currentFolder);
};
function Agui_GoToFolder(galleryID, rootfolder,newfolder) {
    __doPostBack(galleryID,"GoToFolder:" + newfolder);
};
function Agui_CreateFolder(galleryID) {
	folder = document.getElementById('command_NewFolderName');
	folderButton = document.getElementById('command_NewFolderButton');

	if (folder.value == '') {
		alert(_str5);            //创建文件夹时你必须输入一个正确的名称!
		return false;
	}
	folder.disabled = true;
	folderButton.disabled = true;	
	__doPostBack(galleryID,"CreateFolder:" + folder.value);
};
function Agui_UploadFile(galleryID,flg) {
	
	file = document.getElementById('command_UploadFile');
	uploadButton = document.getElementById('command_UploadButton');

	if (file.value == '') {
		alert(_str6);            //您必须选择一个正确的文件!
		return false;
	}
	//file.disabled = true;
	uploadButton.disabled = true;
	__doPostBack(galleryID,"UploadImage"+flg);
};
function Agui_UploadFiles(galleryID,flg)
{
    document.getElementById("filesdiv").style.display="none";
    document.getElementById("uploadState").style.display="block";
    uploadButton = document.getElementById('command_UploadButton');
    uploadButton.disabled = true;
	__doPostBack(galleryID,"UploadAllImage"+flg);
};
function Agui_ResizeGalleryArea() {
	gallery = document.getElementById('Gallery');
	galleryTop = document.getElementById('GalleryTop');
	galleryBottom = document.getElementById('GalleryBottom');
	sideBar = document.getElementById('GallerySideBar');
	
	if (Agui_Browser.isIE) {
		// check window height
		if (document.body.offsetHeight < (galleryTop.offsetHeight + galleryBottom.offsetHeight + sideBar.offsetHeight) ) {
			window.resizeTo(750, galleryTop.offsetHeight + galleryBottom.offsetHeight + sideBar.offsetHeight+100);
		}
				
		gallery.style.height = document.body.offsetHeight - (galleryTop.offsetHeight + galleryBottom.offsetHeight);
		gallery.style.width = document.body.offsetWidth - sideBar.offsetWidth;
	
	} else {
		if (window.innerHeight < (galleryTop.offsetHeight + galleryBottom.offsetHeight + sideBar.offsetHeight) ) {
			window.resizeTo(750, galleryTop.offsetHeight + galleryBottom.offsetHeight + sideBar.offsetHeight+100);
		}
		gallery.style.height =  window.innerHeight - (galleryTop.offsetHeight + galleryBottom.offsetHeight);
		gallery.style.width = window.innerWidth - sideBar.offsetWidth;				
	}	
};
function Agui_DimensionChange(sender) {
	switch (sender.id) {
		default:
		case "img_dim_original": 
			document.getElementById('img_width_custom').value = '';
			document.getElementById('img_height_custom').value = '';
			document.getElementById('img_percentage').value = '';
			
			document.getElementById('img_width_custom').disabled = true;
			document.getElementById('img_height_custom').disabled = true;
			document.getElementById('img_percentage').disabled = true;
			
			Agui_ResetImage();
			break;
		case "img_dim_custom":
			document.getElementById('img_width_custom').value = document.getElementById('img_width').value;
			document.getElementById('img_height_custom').value = document.getElementById('img_height').value;
			document.getElementById('img_percentage').value = '';
			
			document.getElementById('img_width_custom').disabled = false;
			document.getElementById('img_height_custom').disabled = false;
			document.getElementById('img_percentage').disabled = true;			
			break;	
		case "img_dim_percentage":
			document.getElementById('img_width_custom').value = '';
			document.getElementById('img_height_custom').value = '';
			document.getElementById('img_percentage').value = '100';
			
			document.getElementById('img_width_custom').disabled = true;
			document.getElementById('img_height_custom').disabled = true;
			document.getElementById('img_percentage').disabled = false;	
			Agui_SetImageByPercentage();		
			break;	
	}
};
function Agui_SetImageByPercentage() {
	previewImage = document.getElementById('img_preview');
	width = document.getElementById('img_width').value;
	height = document.getElementById('img_height').value;
	percentage = document.getElementById('img_percentage').value;

	previewImage.width = width * percentage / 100;
	previewImage.height = height * percentage / 100;	
};
function Agui_ResetImage() {
	previewImage = document.getElementById('img_preview');
	width = document.getElementById('img_width').value;
	height = document.getElementById('img_height').value;

	previewImage.width = width;
	previewImage.height = height;
};
function Agui_UpdatePreview(sender) {
	src = document.getElementById('img_feedback_message').value;
	if (src == null || src == '') return;
	
	previewImage = document.getElementById('img_preview');
	width = document.getElementById('img_width').value;
	height = document.getElementById('img_height').value;
	customWidth = document.getElementById('img_width_custom').value;
	customHeight = document.getElementById('img_height_custom').value;
	lockRatio = document.getElementById('img_lockRatio').checked;
	
	if (sender.id == 'img_percentage') {
		Agui_SetImageByPercentage();
	} else {
	
		if (lockRatio) {
			if (sender.id == 'img_width_custom') {			
				previewImage.width = customWidth;
				previewImage.height = height * ( customWidth / width);
				document.getElementById('img_height_custom').value = height * ( customWidth / width);
			} else if (sender.id == 'img_height_custom') {
				previewImage.width = width * ( customHeight / height);		
				previewImage.height = customHeight;
				
				document.getElementById('img_width_custom').value = width * ( customHeight / height);
			}
		} else {
			previewImage.width = customWidth;
			previewImage.height = customHeight;
		}
		
	}
};
function Agui_RestorePreview() {
	previewImage = document.getElementById('img_preview');
	previewImage.width = document.getElementById('img_previewWidth').value;
	previewImage.height = document.getElementById('img_previewHeight').value;
	document.getElementById('img_width').value = document.getElementById('img_previewWidth').value;
	document.getElementById('img_height').value = document.getElementById('img_previewHeight').value;	
};
function Agui_FolderClick(theDiv, folderName) {
	Agui_HightlightDiv(theDiv);
	currentFolder = folderName;
	document.getElementById('command_DeleteImageButton').style.display = 'none';
	document.getElementById('command_DeleteFolderButton').style.display = 'block';
	
	document.getElementById('img_feedback_title').innerHTML =_str7;              // "选择文件夹"
	document.getElementById('img_feedback_message').value = folderName;
};
function Agui_PreviewImage(theDiv,filepath,filename,size) {
	if(theDiv==null)
	{
	    if(document.getElementById("img_web_cbx").checked)
	    {
	        document.getElementById("img_web_url").disabled =false;	 
	    }   
	    else
	    {
	        document.getElementById("img_web_url").disabled =true;	 	        
	    }
	}
	else
	{
	    document.getElementById("img_web_cbx").checked=false;   
	    document.getElementById("img_web_url").disabled =true;	 	
	    Agui_HightlightDiv(theDiv);	    
	}
	currentImage = filename;
	document.getElementById('command_DeleteImageButton').style.display = 'block';
	document.getElementById('command_DeleteFolderButton').style.display = 'none';	
	document.getElementById('img_feedback_title').innerHTML =_str8;               // "选择图片"
	document.getElementById('img_feedback_message').value = filename + " (" + size + ")";	

	document.getElementById('img_alt').value = filename;
	document.getElementById('img_border').value = "0";		
	document.getElementById('img_dim_percentage').checked = false;
	document.getElementById('img_dim_custom').checked = false;
	document.getElementById('img_dim_original').checked = true;
	
	var tmpImage=new Image();
	tmpImage.src=filepath + "/" + filename;
	document.getElementById('img_width').value=tmpImage.width;
	document.getElementById('img_height').value=tmpImage.height;
	
	image = document.getElementById('img_preview');
	if(Agui_index_img=="")
	{
	    Agui_index_img=image.src;
	}
	image.src = filepath + "/" + filename;
	image.width=tmpImage.width;
	image.height=tmpImage.height;
	tmpImage=null;
};
function SwapDiv(flg)
{
    var div1=document.getElementById("AguiImageGallery");
    var div2=document.getElementById("loadfilesdiv");
    var div3=document.getElementById("statusdiv");
    var span1=document.getElementById("span1");
    var span2=document.getElementById("span2");    
    var span3=document.getElementById("span3");    
    if(flg==1)
    {
        div1.style.display="none";
        div2.style.display="block";
        div3.style.display="none";
        span1.className="select_span";
        span2.className="unselect_span";
        span3.className="unselect_span";       
    }
    else if(flg==2)
    {
        div1.style.display="block";
        div2.style.display="none";
        div3.style.display="none";       
        span1.className="unselect_span";
        span2.className="select_span";
        span3.className="unselect_span";  
    }
    else
    {
        div1.style.display="none";
        div2.style.display="none";
        div3.style.display="block";        
        span1.className="unselect_span";
        span2.className="unselect_span";
        span3.className="select_span";  
    }
};
function SetWater()
{
    var tmpObj=document.getElementById("IsWaterImage");
    if(tmpObj.checked)
    {
        tmpObj.checked=false;
        tmpObj.value="false";
    }
    else
    {
        tmpObj.checked=true;
        tmpObj.value="true";
    }
};
function UploadImage()
{
    var tmpObj=document.getElementById("IsWaterImage");
    if(tmpObj.checked)
    {
        Agui_UploadFile('AguiImageGallery','true');
    }
    else
    {
        Agui_UploadFile('AguiImageGallery','false');
    }
};
function UploadImages()
{
    var tmpObj=document.getElementById("IsWaterImage");
    var div=document.getElementById("loadfilesdiv"); 
    var isimgall=(div.style.display=="none")?false:true;
    if(tmpObj.checked)
    {
        if(isimgall)
        {
            Agui_UploadFiles('AguiImageGallery','true');
        }
        else
        {
            Agui_UploadFile('AguiImageGallery','true');
        }
    }
    else
    {
        if(isimgall)
        {
            Agui_UploadFiles('AguiImageGallery','false');
        }
        else
        {
            Agui_UploadFile('AguiImageGallery','false');
        }
    }
};

//------------------------------------------------------------
var row_count=0;        //显示的行数
var col_count=4;        //显示的列数
var reset=false;        //重置
var my_tmp_img=new Image();    //临时图象变量
var loaded=false;       //是否已加载
var loading=false;      //是否正在加载
var loadtime=2000;      //规定最长等待时间为3秒
var start_time=0;       //开始上传的时间

//===初试化数据===
function InitData()     
{
	SetCols();
	AddFile();
}
function IsLoading(n)
{
	var flg=false;
	if(loading==true && loaded==false)
	{
		alert("正在加载中请稍后...");
		flg=true;
	}
	if(loading==true && loaded==true)
	{
		alert("图片无法显示,请先上传已选择的图片!\n无法显示的图片也将上传!");
		flg=true;
	}
	if(flg)
	{
		//===设置状态为空===
		SetFileStateForNone(n);
	}
	return loading;
}
//===设置列数需要用到img_table_cols的隐藏元素===
function SetCols()
{
	var img_table_cols=document.getElementById("img_table_cols");
	col_count=img_table_cols.value;
}
//===增加文件===
function AddFile()
{
	//====中间调用AddFrame====
	var table_row="<tr>"
	for(var i=1;i<=col_count;i++)
	{
		var j=i+(row_count*col_count);
		table_row+="<td width=120px height=120px align='center'>";
		table_row+=AddFrame(j);
		table_row+="</td>";
	}
	table_row=table_row+"</tr>";
	row_count++;
	//===将增加的文件添加到filesdiv后面===
	var table_str="<table cellpadding='1' cellspacing='1' border='0' width='100%'>"+table_row+"</table>";
	document.getElementById("filesdiv").insertAdjacentHTML("beforeEnd",table_str+"<br>");
}
//===增加文件的框架供AddFile调用===
function AddFrame(n)
{
	//n代表image的个数
	var toolsimgpath=document.getElementById("toolsimgpath");
	var frm="";
	frm+="<table width='125' height='125' border='0' cellspacing='0' align='center'>";
	frm+="<tr valign='middle'>";
	frm+="	<td width='120' height='120' background='"+toolsimgpath.value+"images/frame_s.jpg' align='center'><table width='100' border='0' cellspacing='0' bgcolor='#FFFFFF'>";
	frm+="	<tr bgcolor='#FFFFFF'>";
	frm+="		<td width='35'>"+n+"</td>";
	frm+="		<td background='"+toolsimgpath.value+"images/browse_ok.jpg'><input id='file"+n+"' name='file' class='file_none'  style='WIDTH:50px; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none; FILTER: alpha(opacity=0);  CURSOR: hand;' onchange='if(!IsLoading("+n+")){SetFile("+n+")}' type='file'></td>";
	frm+="	</tr>";
	frm+="	<tr bgcolor='#FFFFFF' height='70'>";
	frm+="		<td colspan='2' align='center' width='100' style=' table-layout:fixed;'><span id='img_span"+n+"'>NONE</span></td>";
	frm+="	</tr>";
	frm+="	<tr bgcolor='#FFFFFF'>";
	frm+="		<td colspan='2' height='15' align='center'><span id='img_path_txt"+n+"'></span></td>";
	frm+="	</tr>";
	frm+="	</table></td>";
	frm+="</tr>";
	frm+="</table>";
	return frm;
}
//===设置加载文件的特效供LoadFile里元素的change事件调用===
function SetFile(n)
{	
	var imgage_file=document.getElementById("file"+n);
	var img_path=imgage_file.value;
	//img_path=img_path.replace(/\\/g,"/");
	//img_path="file:///"+img_path.replace(":","|");
	var img_span=document.getElementById("img_span"+n);
	var shortname="";
	var _shortname="";
	var cons_ratio=68;
	my_tmp_img.src=img_path;
	var ratio=my_tmp_img.width/my_tmp_img.height;
	if(my_tmp_img.width>my_tmp_img.height)
	{	
		my_tmp_img.width=(my_tmp_img.width>=cons_ratio)?cons_ratio:my_tmp_img.width;
		my_tmp_img.height=cons_ratio/ratio;				
	}
	else
	{
		my_tmp_img.width=cons_ratio*ratio;				
		my_tmp_img.height=(my_tmp_img.height>=cons_ratio)?cons_ratio:my_tmp_img.height;
	}	
	loaded=true;			
	LoadFile(img_path,n);
	start_time=0;
	loading=false;
}
//===设置加载文件的状态===
function SetFileStateForNone(n)
{
	var imgage_file=document.getElementById("file"+n);
	var img_span=document.getElementById("img_span"+n);
	var img_path_txt=document.getElementById("img_path_txt"+n);
	imgage_file.outerHTML="<input  id='file"+n+"' name='file' class='file_none' style='WIDTH:0px; BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-BOTTOM-STYLE: none; FILTER: alpha(opacity=0);  CURSOR: hand;' onchange='if(!IsLoading("+n+")){SetFile("+n+")}' type='file'>";
	img_span.innerHTML="NONE";
	img_path_txt.innerHTML="";
}
//===加载文件的特效===
function LoadFile(img_path,n)
{
	//alert(img.width+"\n"+img.height);
	loaded=false;
	var imgage_file=document.getElementById("file"+n);
	var img_path=imgage_file.value;
	var img_span=document.getElementById("img_span"+n);
	var img_path_txt=document.getElementById("img_path_txt"+n);
	var shortname="";
	var _shortname="";
	if(img_path!="" && img_path.indexOf(".")!=-1)
	{
		var flg=false;
		var imgs =document.all.tags("img");   
		for(var i=0;i<imgs.length;i++)   
		{   
			if(imgs[i].src==my_tmp_img.src)   
			{   
				flg=true;
			}   
		}   	
		if(flg==true)
		{
			alert(_str1);     //您已经选择了这副图片
			SetFileStateForNone(n)
			return;		
		}
	}
	
	shortname=img_path.substring(img_path.lastIndexOf("\\")+1,img_path.length);
	_shortname=shortname;
	if(DecideChar(shortname)>20 || shortname.length>40)
	{
		alert(_str2);         //文件名太长!
		SetFileStateForNone(n)
		return;		
	}
	shortname=shortname.substring(0,shortname.lastIndexOf("."));
	shortname=(shortname.length>=8)?shortname.substring(0,7)+"...":shortname;
	if(my_tmp_img.width==0 || my_tmp_img.height==0)
	{
	    my_tmp_img.width=68;
	    my_tmp_img.height=50;
	}
	if(Agui_Browser.isIE)
	{
	    if(!Agui_Browser.ie7)
	    {
	        img_span.innerHTML="<img id='img"+n+"' src='"+my_tmp_img.src+"' width='"+my_tmp_img.width+"' height='"+my_tmp_img.height+"' alt='"+_shortname+"'>";
	    }
	    else
	    {
	        img_span.innerHTML="<img id='img"+n+"' src='"+my_tmp_img.src+"' width='"+my_tmp_img.width+"' height='"+my_tmp_img.height+"' alt='"+_str3+"'>";          //IE7不能显示<br />本地图片!
	    }
	}
	else
	{
	    img_span.innerHTML="<img id='img"+n+"' src='"+my_tmp_img.src+"' width='"+my_tmp_img.width+"' height='"+my_tmp_img.height+"' alt='"+_str4+"'>";              //不是IE版本<br />不能显示本<br />地图片!
	}
	img_path_txt.innerHTML=shortname;

}
//===检测文件名是否含有中文===
function DecideChar(che_str)
{
	var str="0123456789abcdefghijklmnopqrstuvwxyz_.";
	var len=0;
	var che_str_len=0;
	for(var i=0;i<che_str.length;i++)
	{
		var flg=true;
		for(var j=0;j<str.length;j++)
		{
			if(che_str.substr(i,1)==str.substr(j,1))
			{
				flg=false;
			}
		}
		if(flg==true)
		{
			len++;
		}
	}
	return len;
}
//===刷新===
function Refresh()
{
	reset=true
	if(reset==true)
	{
		window.setTimeout('RefreshFile()',1)
		reset=false;
	}				
}
//===刷新文件===
function RefreshFile()
{
	var imgs =document.all.tags("img");   
	for(var i=1;i<imgs.length;i++)   
	{   
		if(imgs[i].width>imgs[i].height)   
		{   
			if(imgs[i].width>=90)
			{
				imgs[i].width=90;
			}
		}   
		else
		{
			if(imgs[i].height>=90)
			{
				imgs[i].height=90;
			}
		}
	}
}
//===重置文件===
function ReSetFile()
{
	var files_div=document.getElementById("filesdiv");
	var strStatus=document.getElementById("strStatus");
	files_div.innerHTML="";
	strStatus.innerHTML="";
	row_count=0				
	AddFile();
	loaded=false;
	loading=false;
}
window.onload=InitData;
